package com.szh.mysobproject.ui.fragment.user.media

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.szh.mysobproject.R
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.app.AppFragment
import com.szh.mysobproject.databinding.UserFishListFragmentBinding
import com.szh.mysobproject.other.IntentKey
import com.szh.mysobproject.ui.activity.BrowserActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.ShareListAdapter
import com.szh.mysobproject.util.SUNNY_BEACH_SHARE_URL_PRE
import com.szh.mysobproject.util.SimpleLinearSpaceItemDecoration
import com.szh.mysobproject.util.dp
import com.szh.mysobproject.util.loadStateListener
import com.szh.mysobproject.viewmodel.ShareViewModel
import com.szh.mysobproject.widget.StatusLayout
import kotlinx.coroutines.flow.collectLatest

class UserShareListFragment : AppFragment<AppActivity>(), StatusAction {

    private val mBinding by viewBinding<UserFishListFragmentBinding>()
    private val mShareViewModel by viewModels<ShareViewModel>()
    private val adapterDelegate = AdapterDelegate()
    private val mShareAdapter = ShareListAdapter(adapterDelegate)
    private val loadStateListener = loadStateListener(mShareAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }

    override fun getLayoutId(): Int = R.layout.user_fish_list_fragment

    override fun initView() {
        mBinding.rvFishPondList.apply {
            adapter = mShareAdapter
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(SimpleLinearSpaceItemDecoration(4.dp))
        }
    }


    override fun initData() {
        val userId = arguments?.getString(IntentKey.ID) ?: ""
        loadUserShareList(userId)
    }

    private fun loadUserShareList(userId: String) {
        viewLifecycleOwner.lifecycleScope.launchWhenCreated {
            mShareViewModel.loadUserShareList(userId).collectLatest {
                mShareAdapter.submitData(it)
            }
        }
    }

    override fun initEvent() {
        mBinding.refreshLayout.setOnRefreshListener {
            mShareAdapter.refresh()
        }
        mShareAdapter.addLoadStateListener(loadStateListener)
        adapterDelegate.setOnItemClickListener { _, position ->
            val item = mShareAdapter.snapshot()[position] ?: return@setOnItemClickListener
            val url = "$SUNNY_BEACH_SHARE_URL_PRE${item.id}"
            BrowserActivity.start(requireContext(), url)
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.hlFishPondHint

    override fun onDestroyView() {
        super.onDestroyView()
        mShareAdapter.removeLoadStateListener(loadStateListener)
    }

    companion object {
        @JvmStatic
        fun newInstance(userId: String): UserShareListFragment {
            val fragment = UserShareListFragment()
            val args = Bundle().apply {
                putString(IntentKey.ID, userId)
            }
            fragment.arguments = args
            return fragment
        }
    }

}